<?php


namespace app\api\controller;

use app\api\model\Hongbao as HongbaoModel;
use app\api\validate\order\Checkout as CheckoutValidate;

class Hongbao extends Controller
{
    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public $hongbao;
    public $user;
    public function _initialize()
    {
        parent::_initialize();
        $this->hongbao = new HongbaoModel();
        // 用户信息
        $this->user = $this->getUser();
    }
    public function index(){
        //查询已经中将的用户
        $this->hongbao = new HongbaoModel();
        $list = $this->hongbao->getLists();
        return $this->renderSuccess(compact('list'));
    }
    public function hongBaoAdd(){
        $msg = '套路少，红包越大越好';
        $rund = rand(0,13);
        $getTypeArray = $this->getType($rund);
        $type = $getTypeArray['type'];
        $points = 0;
        $money = 0;
        if($getTypeArray['type'] == 0){
            $points = $getTypeArray['num'];
        }else{
            $money = $getTypeArray['num'];
        }
        $resule = $this->hongbao->addHongBao($this->user, $msg, $type, $money, $points);
        if($resule['status'] == 200){
            return $this->renderSuccess(compact('resule'));
        }
        return $this->renderError($resule['msg']);
    }
    public function getType($rund){
        $array = [
            '0' => [
                'type' => 0,
                'num' => 5
            ],
            '1' => [
                'type' => 0,
                'num' => 10
            ],
            '2' => [
                'type' => 0,
                'num' => 15
            ],
            '3' => [
                'type' => 0,
                'num' => 20
            ],
            '4' => [
                'type' => 0,
                'num' => 25
            ],
            '5' => [
                'type' => 0,
                'num' => 30
            ],
            '6' => [
                'type' => 1,
                'num' => '0.3',
            ],
            '7' => [
                'type' => 0,
                'num' => '1',
            ],
            '8' => [
                'type' => 0,
                'num' => '2',
            ],
            '9' => [
                'type' => 0,
                'num' => '3',
            ],
            '10' => [
                'type' => 0,
                'num' => '5',
            ],
            '11' => [
                'type' => 0,
                'num' => '6',
            ],
            '12' => [
                'type' => 0,
                'num' => '9',
            ],
            '13' => [
                'type' => 0,
                'num' => '8',
            ],
        ];
        return $array[$rund];
    }
}